//
//  DetailController.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/22.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit

class DetailController: UIViewController {
    
    var data: AccountDetailModel? {
        didSet {
            if let currentData = data {
                titleLabel.text = "资产编辑"
                nameTextField.text = currentData.name
                nameTextField.isEnabled = false
                nameTextField.backgroundColor = UIColor.la_bg
                amountTextField.text = String(format: "%.2f", currentData.amount)
            }
        }
    }
    
    var name: String {
        get {
            return nameTextField.text!
        }
    }
    
    var amount: String? {
        get {
            if isMoney(amountTextField.text!) {
                return amountTextField.text!
            } else {
                return nil
            }
        }
    }
    
    @IBOutlet weak var contentView: SpringView!

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        layoutUI()
    }
    
    fileprivate func prepareUI() {
        self.contentView.addSubview(titleLabel)
        self.contentView.addSubview(nameTextField)
        self.contentView.addSubview(amountTextField)
    }
    
    
    fileprivate func layoutUI() {
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(20)
            make.height.equalTo(20)
        }
        nameTextField.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(30)
            make.left.equalTo(10)
            make.height.equalTo(40)
        }
        amountTextField.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(nameTextField.snp.bottom).offset(10)
            make.left.equalTo(10)
            make.height.equalTo(40)
        }
    }
    
    
    lazy var titleLabel: UILabel = {
        let object = UILabel()
        object.textColor = .black
        object.font = UIFont.la_Font(ofSize: 15)
        object.textAlignment = .center
        object.text = "资产添加"
        return object
    }()
    
    lazy var nameTextField: UITextField = { [unowned self] in
        let object = UITextField()
        object.delegate = self
        object.layer.cornerRadius = 2
        object.borderStyle = .none
        object.placeholder = "资产名称"
        object.layer.borderColor = UIColor.la_grayer.cgColor
        object.layer.borderWidth = 0.5
        object.font = UIFont.la_Font(ofSize: 15)
        // 为了间隔
        object.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 10, height: 0))
        object.leftViewMode = UITextFieldViewMode.always
        object.returnKeyType = .next
        return object
    }()
    
    lazy var amountTextField: UITextField = { [unowned self] in
        let object = UITextField()
        object.delegate = self
        object.layer.cornerRadius = 2
        object.borderStyle = .none
        object.placeholder = "账户金额"
        object.keyboardType = UIKeyboardType.decimalPad
        object.layer.borderColor = UIColor.la_grayer.cgColor
        object.layer.borderWidth = 0.5
        object.font = UIFont.la_Font(ofSize: 15)
        // 为了间隔
        object.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 10, height: 0))
        object.leftViewMode = UITextFieldViewMode.always
        object.returnKeyType = .done
        return object
    }()
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

extension DetailController: UITextFieldDelegate {
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        var flag = false
        if textField == nameTextField {
            amountTextField.becomeFirstResponder()
        } else {
            amountTextField.resignFirstResponder()
            flag = true
        }
        return flag
    }
}
